using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Threading;
using Decav.Gatsb.Security;
using Decav.Gatsb.Application;
using System.Web.Security;

namespace Decav.Gatsb.Web
{
    /// <summary>
    /// An HTTP module that retrieves user identity information attached to the thread and 
    /// </summary>
    public class SecurityHttpModule : IHttpModule
    {
        #region IHttpModule Members

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.PostAcquireRequestState += new EventHandler(Context_PostAcquireRequestState);
        }


        void Context_PostAcquireRequestState(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session != null)
            {
                Decav.Gatsb.User user = GatsbApplication.Current.CurrentUser;
                if (user != null)
                {
                    GatsbPrincipal principal = new GatsbPrincipal(new GatsbIdentity(user.Id, user.Email.ToString()));
                    Thread.CurrentPrincipal = principal;
                }
            }
        }

        #endregion
    }
}
